package com.chuantou.yuba.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.chuantou.yuba.R;
import com.chuantou.yuba.api.to.CityTO;
import com.chuantou.yuba.api.to.FishingShopTO;
import com.chuantou.yuba.util.Constants;

/**
 * 发布网店界面
 * 
 * @author dl1001
 * 
 */
public class PublishWangDianFragment extends AbsPublishNearFishingFragment {

	private View mContentView;
	private EditText mNameView;
	private EditText mIntroduceView;
	private TextView mIntroduceCountView;
	private RecyclerView mRecyclerView;
	private EditText mContactView;
	private EditText mPhoneView;
	private EditText mShopUrlView;
	private FishingShopTO mShop;

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		mShop = getArguments().getParcelable(Constants.EXTRA_SHOP);
		if (mContentView == null) {
			mContentView = inflater.inflate(R.layout.publish_wangdian, null);
			mNameView = (EditText) mContentView.findViewById(R.id.name);
			mContactView = (EditText) mContentView.findViewById(R.id.contact);
			mPhoneView = (EditText) mContentView.findViewById(R.id.phone);
			mIntroduceView = (EditText) mContentView.findViewById(R.id.introduce);
			mIntroduceCountView = (TextView) mContentView.findViewById(R.id.introduce_count);
			mIntroduceView.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count) {

				}

				@Override
				public void beforeTextChanged(CharSequence s, int start, int count, int after) {

				}

				@Override
				public void afterTextChanged(Editable s) {
					int length = mIntroduceView.getText().length();
					mIntroduceCountView.setText(String.valueOf(200 - length));
				}
			});
			mShopUrlView = (EditText) mContentView.findViewById(R.id.shop_url);
			mRecyclerView = (RecyclerView) mContentView.findViewById(R.id.recyclerView);
			LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
			layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
			mRecyclerView.setLayoutManager(layoutManager);
			mRecyclerView.setAdapter(mImageAdapter);
			if (mShop != null) {
				mNameView.setText(mShop.getName());
				mContactView.setText(mShop.getContact());
				mPhoneView.setText(mShop.getPhone());
				mIntroduceView.setText(mShop.getContent());
				setCoverView(mShop.getImages());
				mShopUrlView.setText(mShop.getShopUrl());
			}
		}
		ViewGroup parentView = (ViewGroup) mContentView.getParent();
		if (parentView != null) {
			parentView.removeView(mContentView);
		}
		return mContentView;
	}

	@Override
	public int getMaxPic() {
		return 1;
	}

	@Override
	public void onSubmit() {
		String name = mNameView.getText().toString().trim();
		if (TextUtils.isEmpty(name)) {
			Toast.makeText(mContext, R.string.input_name_error, Toast.LENGTH_SHORT).show();
			return;
		}
		String contact = mContactView.getText().toString();
		if (TextUtils.isEmpty(contact)) {
			Toast.makeText(mContext, R.string.input_contact_error, Toast.LENGTH_SHORT).show();
			return;
		}
		String phone = mPhoneView.getText().toString().trim();
		if (TextUtils.isEmpty(phone)) {
			Toast.makeText(mContext, R.string.input_telephone_error, Toast.LENGTH_SHORT).show();
			return;
		}
		String introduce = mIntroduceView.getText().toString().trim();
		if (TextUtils.isEmpty(introduce)) {
			Toast.makeText(mContext, R.string.input_introduce_error, Toast.LENGTH_SHORT).show();
			return;
		}
		String shopUrl = mShopUrlView.getText().toString().trim();
		if (TextUtils.isEmpty(shopUrl)) {
			Toast.makeText(mContext, R.string.input_shop_url_error, Toast.LENGTH_SHORT).show();
			return;
		}
		long shopId = mShop == null ? -1 : mShop.getId();
		uploadFishingShop(shopId, 3, null, null, name, introduce, contact, phone, null, shopUrl);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		default:
			super.onClick(v);
			break;
		}
	}

	@Override
	public void onCityPicked(CityTO province, CityTO city) {
	}
}
